package com.framework.loippi.service.impl;

import com.framework.loippi.dao.TSequenceDao;
import com.framework.loippi.entity.TSequence;
import com.framework.loippi.service.TSequenceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;

/**
 * SERVICE - TSequence(序列化)
 *
 * @author longbh
 * @version 2.0
 */
@Service
public class TSequenceServiceImpl extends GenericServiceImpl<TSequence, Long> implements TSequenceService {

    @Autowired
    private TSequenceDao tSequenceDao;


    @Autowired
    public void setGenericDao() {
        super.setGenericDao(tSequenceDao);
    }


    @Override
    public String nextValue(String key) {
        TSequence tSequence = find("cKey", key);
        if (tSequence == null) {
            tSequence = new TSequence();
            tSequence.setCKey(key);
            tSequence.setCValue(1l);
            save(tSequence);
        } else {
            Long value = Long.valueOf(tSequence.getCValue());
            tSequence.setCValue(value + 1);
            update(tSequence);
        }
        DecimalFormat df1 = new DecimalFormat("00000000");
        return df1.format(Long.valueOf(tSequence.getCValue()));
    }

}
